package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.CD;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;


/**
 * @author jwinf
 * 编辑光盘窗体控制类
 */
public class EditCdFrameController extends OperateCdFrameController {
    @FXML
    private Label stateIcon;
    @FXML
    private TextField statetxt;

    // 重写提交方法 实现编辑操作
    @Override
    protected void submit() {
        // 获取组件中的数据
        String code = codetxt.getText();
        String name = nametxt.getText();
        String sort = sorttxt.getText();
        String number = numbertxt.getText();
        // 检验数据
        if(!check(code, "编号为空") || !check(name, "名称为空") || !check(sort, "分类为空") || !check(number, "数量为空")){
            return;
        }
        // 给对象注入值
        cd.setCode(code);
        cd.setName(name);
        cd.setSort(sort);
        cd.setNumber(Integer.parseInt(number));

        // 调用服务层的编辑方法 并根据结果弹出弹窗提示用户
        if(cdService.edit(cd)){
            WindowUtils.showTip("编辑成功", "/icon/SuccessIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");

        }else {
            WindowUtils.showTip("编辑失败", "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
        }
    }

    // 初始化方法 在组件写入数据 给标签设置icon样式
    public void init(CD cd){
        this.cd = cd;
        codetxt.setText(cd.getCode());
        nametxt.setText(cd.getName());
        sorttxt.setText(cd.getSort());
        statetxt.setText(cd.getState());
        numbertxt.setText(String.valueOf(cd.getNumber()));
        Image image = new Image("/icon/StateIcon.png", 30, 30, false, false);
        stateIcon.setGraphic(new ImageView(image));
    }
}
